package org.codeart.price.controller;

import org.codeart.common.dto.PriceRule;
import org.codeart.common.request.EstimatedPriceDTO;
import org.codeart.common.response.PriceResponse;
import org.codeart.common.util.R;
import org.codeart.price.service.PriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/price")
public class PriceController {

    @Autowired
    private PriceService priceService;

    /**
     * 计算预估价格
     */
    @PostMapping("/estimated")
    public R<PriceResponse> estimatedPrice(@RequestBody EstimatedPriceDTO estimatedPriceDTO) {
        String depLongitude = estimatedPriceDTO.getDepLongitude();
        String depLatitude = estimatedPriceDTO.getDepLatitude();
        String destLongitude = estimatedPriceDTO.getDestLongitude();
        String destLatitude = estimatedPriceDTO.getDestLatitude();
        String cityCode = estimatedPriceDTO.getCityCode();
        String vehicleType = estimatedPriceDTO.getVehicleType();
        PriceResponse response = priceService.estimatedPrice(depLongitude, depLatitude, destLongitude, destLatitude, cityCode, vehicleType);
        if (response == null) {
            return R.fail("计价规则不存在");
        }
        return R.ok(response);
    }

    @PostMapping("/rule")
    public R<Void> addRule(@RequestBody PriceRule priceRule) {
        boolean b = priceService.addPriceRule(priceRule);
        if (b) {
            return R.ok();
        }
        return R.fail("计价规则添加失败");
    }

    @PutMapping("/rule")
    public R<Void> edit(@RequestBody PriceRule priceRule) {
        boolean b = priceService.updatePriceRule(priceRule);
        if (b) {
            return R.ok();
        }
        return R.fail("计价规则没有变化");
    }

    /**
     * 判断该城市和对应车型的计价规则是否存在
     * @param priceRule 计价规则
     * @return boolean
     */
    @PostMapping("/rule/exists")
    public R<Boolean> exists(@RequestBody PriceRule priceRule) {
        boolean b = priceService.exists(priceRule);
        return R.ok(b);
    }

    /**
     * 计算实际价格
     * @param distance    距离
     * @param duration    时长
     * @param cityCode    城市码
     * @param vehicleType 车辆类型
     */
    @PostMapping("/calculate-price")
    public R<Double> calculatePrice(@RequestParam("distance") Integer distance,
                                    @RequestParam("duration") Integer duration,
                                    @RequestParam("cityCode") String cityCode,
                                    @RequestParam("vehicleType") String vehicleType) {
        return priceService.calculatePrice(distance, duration, cityCode, vehicleType);
    }

}
